package com.bzxgc.sgwer.service.impl.role;

import com.bzxgc.sgwer.common.util.PageData;
import com.bzxgc.sgwer.dao.role.RoleMapper;
import com.bzxgc.sgwer.entity.role.Role;
import com.bzxgc.sgwer.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addRole(Role role) {
        role.setAddTime(new Date());
        role.setStatus(1);
        roleMapper.addRole(role);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(new Date());
        roleMapper.updateRole(role);
    }

    @Override
    public void updateRoleStatus(Long[] ids, Integer status) {
        roleMapper.updateRoleStatus(ids, status);
    }

    @Override
    public Role getRoleByName(String name) {
        Role role = roleMapper.getRoleByName(name);
        if (role == null) {
            role = new Role();
        }
        return role;
    }

    @Override
    public PageData<Role> getRoleListByPage(Integer pageNum, Integer pageSize, String name) {
        List<Role> roleList = roleMapper.getRoleListByPage(PageData.fromIndex(pageNum, pageSize), pageSize, name);
        Integer totalRecord = roleMapper.getRoleCount(name);
        PageData<Role> pageData = new PageData<>(pageSize, pageNum, totalRecord, roleList);
        return pageData;
    }

    @Override
    public Integer getRoleCount(String name) {
        return roleMapper.getRoleCount(name);
    }

    @Override
    public List<Role> getAllRole() {
        return roleMapper.getAllRole();
    }

    @Override
    public Role getRoleById(Long id) {
        return roleMapper.getRoleById(id);
    }

    @Override
    public List<Role> getRoleByUserId(Long userId) {
        List<Role> list = roleMapper.getRoleByUserId(userId);
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

}
